
package org.globalplatform;

import javacard.framework.*;
import com.wk.javacard.vnative.VN;

class ISDCVM implements CVM{



    /**
    * construct function
    */
    ISDCVM(){

		VN.native_FrameWork_JCSystem_setJCREEntry(this, false);
		//VN.native_OP_initCVM();
    }

	private static final short STATUS_CVM_RESETSTATE = 0x01;
	private static final short STATUS_CVM_BLOCKSTATE = 0x02;
	private static final short STATUS_CVM_RESETANDUNBLOCKSTATE = 0x03;

	private static final short STATUS_CVM_ISACTIVE = 0x10;
	private static final short STATUS_CVM_ISSUBMITTED = 0x20;
	private static final short STATUS_CVM_ISVERIFIED = 0x30;
	private static final short STATUS_CVM_ISBLOCKED = 0x40;



    /** * This method indicates whether the CVM is present and activated.
    * If active the CVM could be in any one of the following states: ACTIVE, INVALID_SUBMISSION,
    * VALIDATED or BLOCKED.
    * @return <code>true</code> if the CVM state is (at least) ACTIVE, <code>false</code> otherwise.
    */
    public boolean isActive(){
		return VN.native_OP_processCVMStatus(STATUS_CVM_ISACTIVE);
    }

    /**
    * This method indicates whether an attempt has been made to compare the CVM value.
    * <p>Note:<ul>
    * <li><em> This method does not differentiate whether the CVM value has been successfully
    * verified or not, i.e. CVM states of VALIDATED or INVALID_SUBMISSION.</em>
    * </ul>
    * @return <code>true</code> if the CVM state is (at least) SUBMITTED, <code>false</code> otherwise.
    */
    public boolean isSubmitted(){
		return VN.native_OP_processCVMStatus(STATUS_CVM_ISSUBMITTED);
    }

    /**
    * This method indicates whether a successful comparison of the CVM value has occurred (CVM state
    * of VALIDATED).
    * @return <code>true</code> if the CVM state is VALIDATED, <code>false</code> otherwise.
    */
    public boolean isVerified(){
		return VN.native_OP_processCVMStatus(STATUS_CVM_ISVERIFIED);
    }

    /**
    * This method indicates whether the CVM is currently BLOCKED.
    * @return <code>true</code> if the CVM state is BLOCKED, <code>false</code> otherwise.
    */
    public boolean isBlocked(){
		return VN.native_OP_processCVMStatus(STATUS_CVM_ISBLOCKED);
    }

    /**
    * This method returns the number of tries remaining for the CVM.
    * This indicates the number of times the CVM value can be incorrectly presented prior
    * to the CVM reaching the state of BLOCKED.
    * @return Tries remaining.
    */
    public byte getTriesRemaining(){
    	return VN.native_OP_getCVMTriesRemaining();
    }

    /**
    * This method changes the CVM value.
    * <p>Notes:<ul>
    * <li><em>The OPEN locates the entry of the current applet context in the Open Platform Registry
    * and verifies that the application has the CVM change privilege.</em>
    * <li><em>The applet is responsible for identifying the format of the CVM value.</em>
    * <li><em>The CVM try counter is reset when changing the CVM value.</em>
    * </ul>
    * @param <code>baBuffer</code> the source byte array containing the CVM value. This buffer must be global.
    * @param <code>sOffset</code> the offset of the CVM value within source byte array.
    * @param <code>bLength</code> the length of the CVM value.
    * @param <code>bFormat</code> the format of the CVM value.
    * @return <code>true</code> if the CVM value was changed, <code>false</code> otherwise.
    */
    public boolean update(byte[] babuffer, short sOffset, byte bLength, byte bFormat){
    	//Added by Chen L 20040317 to catch Exception internal
    	try{
	   		return VN.native_OP_updateCVM_CT();
		}catch(Exception e){
			return false;
		}
    }

    /**
    * This method resets the CVM state to ACTIVE.
    * <p>Notes:<ul>
    * <li><em>The state of the CVM can only be set to ACTIVE from the states INVALID_SUBMISSION or VALIDATED.</em>
    * <li><em>The state of the CVM cannot be set to ACTIVE from BLOCKED.</em>
    * </ul>
    * @return <code>true</code> if the CVM state was reset, <code>false</code> otherwise.
    */
    public boolean resetState(){
   		return VN.native_OP_processCVMStatus(STATUS_CVM_RESETSTATE);
    }

    /**
    * This method sets the CVM state to BLOCKED.
    * @return <code>true</code> if the CVM state was set to BLOCKED, <code>false</code> otherwise.
    */
    public boolean blockState(){
		return VN.native_OP_processCVMStatus(STATUS_CVM_BLOCKSTATE);

    }

    /**
    * This method resets the CVM state from BLOCKED to ACTIVE.
    * <p>Notes:<ul>
    * <li><em>The OPEN locates the entry of the current applet context in the Open Platform Registry and
    * verifies that the application has the CVM change privilege.</em>
    * <li><em>The CVM try counter is reset when unblocking the CVM.</em>
    * </ul>
    * @return <code>true</code> if the CVM state was reset to ACTIVE, <code>false</code> otherwise.
    */
    public boolean resetAndUnblockState(){
		return VN.native_OP_processCVMStatus(STATUS_CVM_RESETANDUNBLOCKSTATE);
    }

    /**
    * This method sets the maximum number of tries for the CVM.
    * <p>Notes:<ul>
    * <li><em>The OPEN locates the entry of the current applet context in the Open Platform Registry
    * and verifies that the application has the CVM change privilege.</em>
    * <li><em>The CVM try counter is reset when setting the maximum number of tries.</em>
    * </ul>
    * @param <code>bTryLimit</code> the maximum number of tries for the CVM.
    * @return <code>true</code> if the maximum number of tries was set, <code>false</code> otherwise.
    */
    public boolean setTryLimit(byte bTryLimit){
    	return VN.native_OP_setCVMTryLimit_CT();
    }

    /**
    * This method compares the stored CVM value with the one passed as parameter.
    * <p>Notes:<ul>
    * <li><em>If the value passed as parameter is not in the same format as the CVM value,
    * the value passed as parameter must be converted prior to comparing.</em>
    * <li><em>If the comparison is successful, the try counter must be reset and the CVM state
    * must be set to VALIDATED.</em>
    * <li><em>If the comparison is unsuccessful, the try counter must be updated and the
    * CVM state must be set to INVALID_SUBMISSION.</em>
    * <li><em>If the maximum number of tries has been reached, the CVM state must be set to BLOCKED.</em>
    * </ul>
    * @param <code>baBuffer</code> the source byte array containing the submitted CVM value. This buffer must be global.
    * @param <code>sOffset</code> the offset of the submitted CVM value within source byte array.
    * @param <code>bLength</code> the length of the submitted CVM value.
    * @param <code>bFormat</code> the format of the submitted CVM value.
    * @return value indicating whether the comparison was successful or not. Values other than CVM_SUCCESS (0) or
    * CVM_FAILURE (-1) are Reserved for Future Use.
    */
    public short verify(byte[] baBuffer, short sOffset, byte bLength, byte bFormat){
    	//Added by Chen L 20040317 to catch Exception internal
    	try{
	    	return VN.native_OP_verifyCVM_CT();
		}catch(Exception e){
			return -1;
		}
    }


}


